<?php

define('ACC', true);
require ('../includes/init.php');
$attrCat = new AttrCatModel();

if($_GET['act'] == 'insert' || $_GET['act'] == 'update'){
	$act = trim($_GET['act']);
	
	$data = array();
	$data['attr_name'] = $_POST['attr_name'];
	$data['cat_id'] = $_POST['cat_id'];
	$data['attr_index'] = $_POST['attr_index'];
	$data['is_linked'] = $_POST['is_linked'];
	$data['attr_type'] = $_POST['attr_type'];

	$attr_input_type = $_POST['attr_input_type'];
	if($attr_input_type == 0){
		$data['attr_input_type'] = '手工录入';
	}else if($attr_input_type == 1){
		$data['attr_input_type'] = '列表中选择';
	}else{
		$data['attr_input_type'] = '多行文本框';
	}
	
	if($act == 'insert'){
		$attr_values = $_POST['attr_values'];
	}else{
		$attr_values = $_POST['attr_values2'];
	}
	
	$attr_desc = str_replace("\n", ",", $attr_values);

	$arr = explode(",", $attr_desc);

	$str = '';
	foreach($arr as $val){
		$val = trim($val);
		$str .= $val . ',';
	}
	$str = substr($str, 0, -1);

	$data['attr_values'] = $str;
	
	if($act == 'insert'){
		if($attrCat->attr_insert($data)){
			$desc = '添加' . $data['attr_name'] . '成功';
			$url = 'attribute.php?act=list&cat_id=' . $data['cat_id'];
			$link = '商品类型';
			admin_msg($smarty, $desc, $url, $link);
			exit;
		}else{
			echo '添加商品类型失败！';
			exit;
		}
	}else{
		if(isset($_GET['attr_id']) && (($attr_id = ($_GET['attr_id'] + 0)) > 0)){
			$where = "attr_id = '$attr_id'";
			if($attrCat->attr_update($data, $where)){
				$desc = '修改' . $data['attr_name'] . '成功';
				$url = 'attribute.php?act=list&cat_id=' . $data['cat_id'];
				$link = '商品类型';
				admin_msg($smarty, $desc, $url, $link);
				exit;
			}else{
				echo '修改商品类型失败！';
				exit;
			}
		}else{
			echo '修改商品类型失败！';
			exit;
		}
	}
	
}


